/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.service.render.exporter;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.util.JRSaver;

import java.io.ByteArrayOutputStream;
import java.util.*;

/**
 * Custom Jasper Exporter to export a collection of {@link JasperPrint} objects as a single JasperPrint byte-Array. Necessary
 * to print documents.
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 20:58 07.11.11
 *
 * @author Burt Parkers
 * @version 1.0
 */
class JasperPrintExporter implements JRExporter {

    /** Collection of supported and required Parameters. */
    private Collection<JRExporterParameter> supportedParameterCollection;

    /** Parameter map. */
    private Map<JRExporterParameter, Object> parameterMap;

    /** Constructor, initializes the lists. */
    public JasperPrintExporter() {
        supportedParameterCollection = new HashSet<>();
        supportedParameterCollection.add(JRExporterParameter.JASPER_PRINT_LIST);
        supportedParameterCollection.add(JRExporterParameter.OUTPUT_STREAM);

        parameterMap = new HashMap<>();
    }

    @Override
    public void setParameter(JRExporterParameter parameter, Object value) {
        if (supportedParameterCollection.contains(parameter)) {
            parameterMap.put(parameter, value);
        } else {
            throw new RuntimeException("The Exporter JasperPrintExporter does not supports this parameter type: " + parameter);
        }
    }

    @Override
    public Object getParameter(JRExporterParameter parameter) {
        return parameterMap.get(parameter);
    }

    @Override
    public void setParameters(Map parameters) {
        for (Object parameter : parameters.keySet()) {
            parameterMap.put((JRExporterParameter) parameter, parameters.get(parameter));
        }
    }

    @Override
    public Map getParameters() {
        return Collections.unmodifiableMap(parameterMap);
    }

    @Override
    public void exportReport() throws JRException {
        Object outputStreamValue = parameterMap.get(JRExporterParameter.OUTPUT_STREAM);
        if (outputStreamValue == null) {
            throw new RuntimeException("Parameter JRExporterParameter.OUTPUT_STREAM must be set!");
        }
        Object printListValue = parameterMap.get(JRExporterParameter.JASPER_PRINT_LIST);
        if (printListValue == null) {
            throw new RuntimeException("Parameter JRExporterParameter.JASPER_PRINT_LIST must be set!");
        }
        ByteArrayOutputStream baos = (ByteArrayOutputStream) outputStreamValue;
        Collection<JasperPrint> printList = (Collection<JasperPrint>) printListValue;
        mergeJrprint(printList, baos);
    }

    /**
     * Merges a collection of {@link JasperPrint} objects and writes the result to the given {@link ByteArrayOutputStream}.
     *
     * @param jasperPrintCollection the collection of {@link JasperPrint} objects.
     * @param baos the {@link ByteArrayOutputStream}.
     * @throws JRException if the merge process fails.
     */
    private void mergeJrprint(Collection<JasperPrint> jasperPrintCollection, ByteArrayOutputStream baos)
        throws JRException {
        JasperPrint rootPrintObject = null;
        for (JasperPrint jasperPrint : jasperPrintCollection) {
            if (rootPrintObject == null) {
                rootPrintObject = jasperPrint;
            } else {
                final List pages = jasperPrint.getPages();
                for (final Object page : pages) {
                    rootPrintObject.addPage((JRPrintPage) page);
                }
            }
        }
        JRSaver.saveObject(rootPrintObject, baos);
    }
}
